.\" Copyright (c) 2009-2018 Julien Nadeau Carriere <vedge@csoft.net>
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\" 
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
.\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
.\" (INCLUDING BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd September 16, 2009
.Dt AG_CUSTOMEVENTLOOP 3
.Os
.ds vT Agar API Reference
.ds oS Agar 1.5
.Sh NAME
.Nm AG_CustomEventLoop
.Nd agar GUI custom event loop example
.Sh SYNOPSIS
.Bd -literal
#include <agar/core.h>
#include <agar/gui.h>
.Ed
.Sh DESCRIPTION
In general, most Agar GUI applications will use the standard Agar-Core
event loop,
.Xr AG_EventLoop 3 ,
since it uses the most efficient event-notification mechanism available for
the target platform (e.g.,
.Xr kqueue 2 ,
.Xr select 2 ,
etc.)
.Pp
However, Agar-GUI applications are not required to use
.Fn AG_EventLoop
at all, and may use an application-specific event loop routine.
To simplify writing of custom event loops, Agar provides the
.Xr AG_GetNextEvent 3 ,
.Xr AG_ProcessEvent 3
and
.Xr AG_ProcessTimeouts 3
functions.
Low-level driver events are represented by the transparent, driver-independent
.Ft AG_DriverEvent
structure (see
.Xr AG_Driver 3
for details and examples).
.Sh CUSTOM EVENT LOOP EXAMPLE
The following event loop redraws Agar windows whenever needed (the redraw
frequency being limited to some nomimal frame rate), processes input events,
and executes timer callbacks (using an inefficient delay loop):
.Bd -literal
void
MY_GenericEventLoop(void *obj)
{
	AG_Window *win;
	Uint32 t1, t2;

	t1 = AG_GetTicks();
	for (;;) {
		t2 = AG_GetTicks();
		if (t2 - t1 >= rNom) {
			AG_WindowDrawQueued();

			t1 = AG_GetTicks();
			rCur = rNom - (t1-t2);
			if (rCur < 1) { rCur = 1; }
		} else if (AG_PendingEvents(NULL)) {
			AG_DriverEvent dev;

			if (AG_GetNextEvent(NULL, &dev) == 1)
				AG_ProcessEvent(NULL, &dev);
		} else {
			AG_ProcessTimeouts(t2);
			AG_Delay(1);
		}
		AG_WindowProcessQueued();
	}
}
.Ed
.Sh SEE ALSO
.Xr AG_Delay 3 ,
.Xr AG_Driver 3 ,
.Xr AG_Event 3 ,
.Xr AG_EventLoop 3 ,
.Xr AG_Timer 3 ,
.Xr AG_Window 3
.Sh HISTORY
The
.Nm
routine first appeared in Agar 1.0.
The
.Fn AG_GetNextEvent
interface appeared in Agar 1.4.
